package com.hf.servlet;

import java.awt.Color;
import java.awt.Font;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.jms.Session;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


@WebServlet("/code.do")
public class ValidateCodeServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
    		throws ServletException, IOException {
    	// TODO Auto-generated method stub
    	resp.setCharacterEncoding("utf-8");
    	resp.setContentType("image/jpeg");//������ӦContentType����
    	ServletOutputStream out = resp.getOutputStream();//��ȡһ��һ�����������������
    	int width = 70;
    	int height = 30;
    	//��������ͼ��
    	BufferedImage imgbuf = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    	Graphics2D  g = imgbuf.createGraphics();//����һ֧����
    	g.fillRect(0, 0, width, height);//����ͼ����״���߶�
    	g.setColor(getRandColor(200,250));//���ñ�����ɫ
    	g.setFont(new Font("Times News Roman",Font.PLAIN,18));
    	//�������0-9��4λ��
    	Random r = new Random();
    	//������
	   	g.setColor(getRandColor(160, 250));
		for(int i=0;i<100;i++){
			int x = r.nextInt(width);
			int y = r.nextInt(height);
			int x1 = r.nextInt(12);
			int y1 = r.nextInt(12);
			g.drawOval(x, y, x+x1, y+y1);
		}
		g.setColor(getRandColor(0, 150));
		for(int i=0;i<100;i++){
			int x = r.nextInt(width);
			int y = r.nextInt(height);
			int x1 = r.nextInt(12);
			int y1 = r.nextInt(12);
			g.drawOval(x, y, 0, 0);
		}
		String code1 = "";
	    for(int i=0;i<4;i++){
	//    	String rand =   String.valueOf(r.nextInt(10));	    	
	    	//������ֺ���ĸ	
	    	int rand;
			int cnt = 26 * 2 + 10;
			int v = r.nextInt(cnt);
			if (v < 10)
				rand = '0' + v;
			else if (v < 36)
				rand = v - 10 +  'A';
			else
				rand = v - 36 +  'a';		
			g.setColor(new Color(20+r.nextInt(110),20+r.nextInt(110),20+r.nextInt(110)));
			g.drawString(String.valueOf((char)rand), 16*i+6, 22);
			code1 += String.valueOf((char)rand);
	    }
	    HttpSession session = req.getSession();
	    session.setAttribute("code1", code1);	    
	    ImageIO.write(imgbuf, "jpeg", out);
	    out.close();
    }
    
    /**
     * ��ȡ�ƶ���Χ�����ɫ
     * @param fc
     * @param bc
     * @return
     */
    
    private Color getRandColor(int fc,int bc){
    	Random random = new Random();
    	if(fc>255){
    		fc=255;
    	}
    	if(fc<0){
    		fc=0;
    	}
    	if(bc>255){
    		bc=255;
    	}
    	if(bc<0){
    		bc=0;
    	}
    	int r = fc+random.nextInt(bc-fc);
    	int g = fc+random.nextInt(bc-fc);
    	int b = fc+random.nextInt(bc-fc);
    	
    	return new Color(r,g,b);
    }

}